package com.syqc.monitor.ui.realalarm

import android.os.Bundle
import android.transition.Explode
import android.view.Window
import androidx.fragment.app.Fragment
import autodispose2.autoDispose
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.player.PlayerFactory
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.entity.AlarmMedia
import com.syqc.interfaces.SimplePageChangeListener
import com.syqc.monitor.databinding.ActivityMediaInfoBinding
import com.syqc.monitor.databinding.IncludeViewpagerMatchBinding
import com.syqc.monitor.ui.list.MViewPagerAdapter
import com.syqc.net.ServiceHelper
import com.syqc.utils.Keys
import com.syqc.videos.AlarmPlayerFragment
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager


/**
 *@Author Jasper
 *@Time   2020/12/1
 *@Desc   主动安全报警详情
 */
class MediaInfoActivity :
    BaseBarActivity<ActivityMediaInfoBinding>(ActivityMediaInfoBinding::inflate) {
    private lateinit var alarmVideoFragment: AlarmPlayerFragment
    private lateinit var alarmPhotoFragment: AlarmPhotoFragment
    private lateinit var mViewPagerVB: IncludeViewpagerMatchBinding
    private var title: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)
        window.enterTransition = Explode()
        window.exitTransition = Explode()
        super.onCreate(savedInstanceState)
        title = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        ToolbarHelper.setToolbar(this, title)
        GSYVideoManager.releaseAllVideos()
        //EXOPlayer内核，支持格式更多
        PlayerFactory.setPlayManager(Exo2PlayerManager::class.java)
    }

    override fun initView() {
        mViewPagerVB = IncludeViewpagerMatchBinding.bind(binding.root)
        val fragments = ArrayList<Fragment>()
        alarmVideoFragment = AlarmPlayerFragment()
        alarmVideoFragment.setTitle(title)
        alarmPhotoFragment = AlarmPhotoFragment()
        fragments.add(alarmVideoFragment)
        fragments.add(alarmPhotoFragment)

        val pagerAdapter = MViewPagerAdapter(fragments, supportFragmentManager)
        mViewPagerVB.mViewPager.adapter = pagerAdapter
        mViewPagerVB.mViewPager.postDelayed({
            val data: ArrayList<AlarmMedia> =
                intent.getParcelableArrayListExtra(Keys.INTENT_DATA) ?: ArrayList()
            val arrayVideosList = ArrayList<AlarmMedia.MediaInfo>()
            val arrayPhotosList = ArrayList<AlarmMedia.MediaInfo>()
            for (entity: AlarmMedia in data) {
                when (entity.mediaType) {
                    "2" -> arrayVideosList.addAll(entity.mediaList)
                    "0" -> arrayPhotosList.addAll(entity.mediaList)
                }
            }
            val entity =
                if (arrayVideosList.isNotEmpty()) arrayVideosList[0] else arrayPhotosList[0]

            entity.teamName = intent.getStringExtra(Keys.INTENT_TEAM_NAME) ?: ""
            alarmVideoFragment.setVideoData(arrayVideosList, entity)
            alarmPhotoFragment.setVideoData(arrayPhotosList, entity)
            val carId = intent.getStringExtra(Keys.INTENT_CAR_ID)
            carId?.let { getCarInfo(it) }
        }, 300L)

    }

    override fun setListener() {
        mViewPagerVB.mViewPager.addOnPageChangeListener(object : SimplePageChangeListener {
            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    binding.rdbMediaVideo.isChecked = true
                } else {
                    binding.rdbMediaPhoto.isChecked = true
                }
            }
        })
        binding.rdbMediaVideo.setOnClickListener {
            mViewPagerVB.mViewPager.currentItem = 0
        }
        binding.rdbMediaPhoto.setOnClickListener {
            mViewPagerVB.mViewPager.currentItem = 1
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        if (alarmVideoFragment.onBackPressed()) {
            return
        }
        finish()
    }

    fun getCarInfo(carId: String) {
        ServiceHelper.getRepository().getCarInfo(carId).autoDispose(scopeProvider).subscribe({
            alarmVideoFragment.setTeamName(it.teamName ?: "")
            alarmPhotoFragment.setTeamName(it.teamName ?: "")
        }, {})
    }

}